﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Models;
using DevExpress.XtraGrid.Columns;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class TourGroupLookForm : DevExpress.XtraEditors.XtraForm
    {
        public DataView _TourGroupDataTable { get; set; }
        //是否是查看
        public bool _LookupState { get; set; }
        
        private DataView _ScheduleDataView = null;
        private string _Remark = "";

        public TourGroupLookForm(string tourgroupId)//DataView tourGroupDataTable)
        {
            InitializeComponent();


            _TourGroupDataTable = TeamGroupCtrl.GetTeamGroupRowData(tourgroupId).DefaultView;
            //_Position = rowID;
            //界面初始化时，备注窗口隐藏，没有值，将备注值存在_Remark中
           _Remark = _TourGroupDataTable[0]["Remark"].ToString();
        }

        #region 窗口初始化
        //窗口Load事件
        private void TourGroupScheduleForm_Load(object sender, EventArgs e)
        {
            #region 查看状态处理
            
            //pricePanel.Enabled = false;
            //pricePanel1.Enabled = false;
            
            btnPrice.Enabled = false;
            btnTourGroupState.Enabled = false;
            btnAdd.Enabled = false;
            btnDelete.Enabled = false;
            btnSalePrice.Enabled = false;
            btnCash.Enabled = false;
            btnChangeNotify.Enabled = false;


            baseInfoPanel.Enabled = false;
            ScheduleGridView.OptionsBehavior.Editable = false;
            
            #endregion
            //初始化团基本信息显示
            InitGui();
            //初始化计划内容
            LoadScheduleGridViewData();


            #region 团状态按钮处理

            DataRowView drv = (BindingContext[_TourGroupDataTable].Current as DataRowView);
            switch (TeamGroupCtrl.GetTourGroupState(lblTourGroupID.Text))
            {
                case TeamStateEnum.计划:
                    btnTourGroupState.Text = "成行";
                    #region 团的费用状态
                    tbBudgeTotalCost.EditValue = drv["BudgeTotalCost"];
                    tbBudgeTotalPrice.EditValue = drv["BudgeTotalPrice"];
                    tbRealTotalCost.EditValue = drv["RealTotalCost"];
                    tbRealTotalPrice.EditValue = drv["RealTotalPrice"];
                    #endregion
                    break;
                case TeamStateEnum.成行:
                    btnTourGroupState.Text = "审核";
                    #region 团的费用状态
                    tbBudgeTotalCost.EditValue = drv["BudgeTotalCost"];
                    tbBudgeTotalPrice.EditValue = drv["BudgeTotalPrice"];
                    tbRealTotalCost.EditValue = drv["RealTotalCost"];
                    tbRealTotalPrice.EditValue = drv["RealTotalPrice"];
                    #endregion
                    //tbTourgroupName.Enabled = false;
                    //tbCountry.Enabled = false;
                    //tbCurrencyAmount.Enabled = false;
                    //tbExchangeRate.Enabled = false;
                    //tbSaleCost.Enabled = false;
                    break;
                case TeamStateEnum.取消:
                    //btnAdd.Enabled = false;
                    //btnDelete.Enabled = false;
                    //btnTourGroupState.Enabled = false;
                    //baseInfoPanel.Enabled = false;
                    //ScheduleGridView.OptionsBehavior.Editable = false;
                    break;
                case TeamStateEnum.审核:
                case TeamStateEnum.终结:
                    //btnAdd.Enabled = false;
                    //btnDelete.Enabled = false;
                    //btnTourGroupState.Enabled=false;
                    //baseInfoPanel.Enabled = false;
                    //mbRemark.Enabled = false;
                    //ScheduleGridView.OptionsBehavior.Editable = false;
                    break;
                default:
                    break;
            }

            #endregion
        }

        //计划行程数据加载
        private void LoadScheduleGridViewData()
        {
            _ScheduleDataView = new DataView(TeamGroupCtrl.GetTourGroupSchedule(lblTourGroupID.Text).Tables[0]);
            scheduleGridControl.DataSource = _ScheduleDataView;

            ScheduleGridView.FocusedRowHandle = ScheduleGridView.RowCount - 1;

        }

        //初始化界面内容
        private void InitGui()
        {

            //团状态信息
            lblTourGroupID.DataBindings.Add("Text", _TourGroupDataTable, "TourgroupID");
            lblTourGroupState.DataBindings.Add("Text", _TourGroupDataTable, "TourgroupState");


            tbCountry.DataBindings.Add("EditValue", _TourGroupDataTable, "Country");
            lblCountryCode.DataBindings.Add("Text", _TourGroupDataTable, "CountryCode");
            tbStartDate.DataBindings.Add("EditValue", _TourGroupDataTable, "StartDate");
            tbEndDate.DataBindings.Add("EditValue", _TourGroupDataTable, "EndDate");
            tbNightNum.DataBindings.Add("EditValue", _TourGroupDataTable, "StayOver");
            tbAdultNum.DataBindings.Add("EditValue", _TourGroupDataTable, "AdultNum");
            tbChildNum.DataBindings.Add("EditValue", _TourGroupDataTable, "ChildNum");
            tbTourgroupName.DataBindings.Add("EditValue", _TourGroupDataTable, "TourgroupName");
            cbGuesttype.DataBindings.Add("EditValue", _TourGroupDataTable, "Guesttype");
            cbServicetype.DataBindings.Add("EditValue", _TourGroupDataTable, "Servicetype");
            cbTourgrouptype.DataBindings.Add("EditValue", _TourGroupDataTable, "Tourgrouptype");
            tbAccompanyNum.DataBindings.Add("EditValue", _TourGroupDataTable, "AccompanyNum");
            tbCustomerCode.DataBindings.Add("EditValue", _TourGroupDataTable, "CustomerCode");
            lblCustomerName.DataBindings.Add("Text", _TourGroupDataTable, "CustomerName");
            cbOwnerA.DataBindings.Add("EditValue", _TourGroupDataTable, "OwnerA");
            cbOwnerB.DataBindings.Add("EditValue", _TourGroupDataTable, "OwnerB");
            ckHasGuide.DataBindings.Add("Checked", _TourGroupDataTable, "HasGuide");
            tbBuildGroupDate.DataBindings.Add("EditValue", _TourGroupDataTable, "BuildGroupDate");
            mbRemark.DataBindings.Add("EditValue", _TourGroupDataTable, "Remark");

            tbSaleCost.DataBindings.Add("EditValue", _TourGroupDataTable, "SaleCost");
            tbSalePrice.DataBindings.Add("EditValue", _TourGroupDataTable, "SalePrice");
            tbSaleTotalCost.DataBindings.Add("EditValue", _TourGroupDataTable, "SaleTotalCost");
            tbSaleTotalPrice.DataBindings.Add("EditValue", _TourGroupDataTable, "SaleTotalPrice");
            tbBudgeTotalCost.DataBindings.Add("EditValue", _TourGroupDataTable, "BudgeTotalCost");
            tbBudgeTotalPrice.DataBindings.Add("EditValue", _TourGroupDataTable, "BudgeTotalPrice");
            tbRealTotalCost.DataBindings.Add("EditValue", _TourGroupDataTable, "RealTotalCost");
            tbRealTotalPrice.DataBindings.Add("EditValue", _TourGroupDataTable, "RealTotalPrice");

            tbCurrency.DataBindings.Add("EditValue", _TourGroupDataTable, "Currency");
            tbCurrencyAmount.DataBindings.Add("EditValue", _TourGroupDataTable, "CurrencyAmount");
            tbExchangeRate.DataBindings.Add("EditValue", _TourGroupDataTable, "ExchangeRate");
            tbPriceMethod.DataBindings.Add("EditValue", _TourGroupDataTable, "PriceMethod");

            string rowState = _TourGroupDataTable[0]["TourgroupRowState"].ToString();//.Rows[_Position]["TourgroupRowState"].ToString();
            tbTourgroupRowState.Checked = (rowState == "2");
        } 
        #endregion

        #region 窗口底部系列按钮事件处理

        //打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            new SchedulePrintForm(lblTourGroupID.Text).ShowDialog(this);
        }
        //备注
        private void btnRemark_Click(object sender, EventArgs e)
        {
            panelRemark.Visible = true;
        }
        private void btnRemarkClose_Click(object sender, EventArgs e)
        {
            panelRemark.Visible = false;
        }
        /// <summary>
        /// 变更通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeNotify_Click(object sender, EventArgs e)
        {
            ChangeNotifyForm changeNofityForm = new ChangeNotifyForm(lblTourGroupID.Text);
            changeNofityForm.ShowDialog(this);
        }
        /// <summary>
        /// 客人列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGuestList_Click(object sender, EventArgs e)
        {
            GuestListForm guestListForm = new GuestListForm(lblTourGroupID.Text);
            guestListForm.ShowDialog(this);
        }

        private void btnLookGuide_Click(object sender, EventArgs e)
        {
            new GuideStatusLookForm(lblTourGroupID.Text).ShowDialog(this);
        }

        private void btnCash_Click(object sender, EventArgs e)
        {
            new CashApplyForm(lblTourGroupID.Text).ShowDialog(this);
        }
        /// <summary>
        /// 查看销售报价信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSalePrice_Click(object sender, EventArgs e)
        {
            //new SalePriceForm(lblTourGroupID.Text).ShowDialog(this);
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnPrice_Click(object sender, EventArgs e)
        {
            new TeamGroupExpenseManagerForm(_ScheduleDataView, lblTourGroupID.Text, lblTourGroupState.Text).ShowDialog(this);
        }
  
        #endregion

        #region 备注框可移动
        Point mouse_offset;

        private void panelRemark_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
        }

        private void panelRemark_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
            }
        }
        #endregion


    }
}
